<?php

class Properti_Baru extends CI_Controller {

    var $_public_view;

    function __construct() {
        parent::__construct();
        $this->load->model('model_listing', 'mlisting');
        $this->load->model('model_properti_baru', 'mpropbaru');
        $this->load->helper('url', 'site');
        $this->load->helper('form');
        $this->load->library('session');
        $this->config->load('artyproperty');
        $this->_public_view = $this->config->item('public_view');
        $this->session->set_userdata('page', 3);
    }

    function index() {
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        $id = "";
        if ($islogin) {
            $user = $this->session->userdata('username');
            $id = $this->session->userdata('user_id');
        }

        $data_output = array();

        $sql = "SELECT * FROM provinsi";
        $query = $this->db->query($sql);
        $provinsi = $query->result();

        $sql = "SELECT * FROM kota";
        $query = $this->db->query($sql);
        $kota = $query->result();

        $sql = "select * from properti_baru order by tgl_upload desc limit 6";
        $query = $this->db->query($sql);
        $result = $query->result();
        $count = 0;
        foreach ($result as $row) {
            $data_output['prop_baru'][$count]['id'] = $row->id_properti_baru;
            $data_output['prop_baru'][$count]['judul'] = $row->judul;
            $data_output['prop_baru'][$count]['kota'] = $row->kota;
            $data_output['prop_baru'][$count]['provinsi'] = $row->provinsi;
            $data_output['prop_baru'][$count]['is_premium'] = $row->isPremium;
            $data_output['prop_baru'][$count]['developer_id'] = $row->developer_id;
            $data_output['prop_baru'][$count]['tgl_upload']= $row->tgl_upload;

            //foto properti baru
            $sql = "select * from media where media.properti_baru_id = ?";
            $query = $this->db->query($sql, $row->id_properti_baru);
            $data_output['prop_baru'][$count]['picture'] = "";
            if ($query->num_rows() == 1) {
                $pic_prop_baru = $query->row();
                $data_output['prop_baru'][$count]['picture'] = $pic_prop_baru->url;
            }

            $sql = "select * from developer where developer.developer_id = ?";
            $query = $this->db->query($sql, $row->developer_id);
            $dev = $query->row();
            $data_output['prop_baru'][$count]['nama_developer'] = $dev->nama;

            $count++;
        }

        $data = array(
            'output' => $data_output,
            'page_title' => 'Properti Baru',
            'page_content' => 'properti_baru',
            'isLogin' => $islogin,
            'user' => $user,
            'provinsi' => $provinsi,
            'kota' => $kota);
        $this->load->view($this->_public_view, $data);
    }

    function cari($page = 1) {
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        $id = "";
        if ($islogin) {
            $user = $this->session->userdata('username');
            $id = $this->session->userdata('user_id');
        }

        $sql = "SELECT * FROM properti_baru, tipe_properti_baru WHERE tipe = tipe_id
            AND judul LIKE '%" . $this->input->get('keyword', TRUE) . "%'";

        $sqltipe = "";

        if ($this->input->get('rumah', TRUE) == 1) {
            $sqltipe = "tipe = 1";
        }

        if ($this->input->get('apartemen', TRUE) == 1) {
            if (strlen($sqltipe) == 0) {
                $sqltipe = "tipe = 2";
            } else {
                $sqltipe = $sqltipe . " OR tipe = 2";
            }
        }
        if ($this->input->get('villa', TRUE) == 1) {
            if (strlen($sqltipe) == 0) {
                $sqltipe = "tipe = 3";
            } else {
                $sqltipe = $sqltipe . " OR tipe = 3";
            }
        }
        if ($this->input->get('condotel', TRUE) == 1) {
            if (strlen($sqltipe) == 0) {
                $sqltipe = "tipe = 4";
            } else {
                $sqltipe = $sqltipe . " OR tipe = 4";
            }
        }
        if ($this->input->get('komersial', TRUE) == 1) {
            if (strlen($sqltipe) == 0) {
                $sqltipe = "tipe = 5";
            } else {
                $sqltipe = $sqltipe . " OR tipe = 5";
            }
        }
        if (strlen($sqltipe) != 0) {
            $sql = $sql . " AND (" . $sqltipe . ")";
        }

        $query = $this->db->query($sql);
        $result = $query->result();
        $count = 0;
        foreach ($result as $row) {
            $data_output['prop_baru'][$count]['id'] = $row->id_properti_baru;
            $data_output['prop_baru'][$count]['judul'] = $row->judul;
            $data_output['prop_baru'][$count]['kota'] = $row->kota;
            $data_output['prop_baru'][$count]['provinsi'] = $row->provinsi;
            $data_output['prop_baru'][$count]['is_premium'] = $row->isPremium;
            $data_output['prop_baru'][$count]['developer_id'] = $row->developer_id;

            //foto properti baru
            $sql = "select * from media where media.properti_baru_id = ?";
            $query = $this->db->query($sql, $row->id_properti_baru);
            $data_output['prop_baru'][$count]['picture'] = "";
            if ($query->num_rows() == 1) {
                $pic_prop_baru = $query->row();
                $data_output['prop_baru'][$count]['picture'] = $pic_prop_baru->url;
            }

            $sql = "select * from developer where developer.developer_id = ?";
            $query = $this->db->query($sql, $row->developer_id);
            $dev = $query->row();
            $data_output['prop_baru'][$count]['nama_developer'] = $dev->nama;

            $count++;
        }

        $data = array(
            'page_title' => 'Hasil Pencarian Properti Baru',
            'page_content' => 'properti_baru',
            'isLogin' => $islogin,
            'user' => $user,
            'output' => $data_output);
        $this->load->view($this->_public_view, $data);
    }
    function properti_premium(){
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        $id = "";
        if ($islogin) {
            $user = $this->session->userdata('username');
            $id = $this->session->userdata('user_id');
        }

        $data_output = array();

        $sql = "SELECT * FROM provinsi";
        $query = $this->db->query($sql);
        $provinsi = $query->result();

        $sql = "SELECT * FROM kota";
        $query = $this->db->query($sql);
        $kota = $query->result();

        $sql = "select * from properti_baru order by tgl_upload desc limit 6";
        $query = $this->db->query($sql);
        $result = $query->result();
        $count = 0;
        foreach ($result as $row) {
            $data_output['prop_baru'][$count]['id'] = $row->id_properti_baru;
            $data_output['prop_baru'][$count]['judul'] = $row->judul;
            $data_output['prop_baru'][$count]['kota'] = $row->kota;
            $data_output['prop_baru'][$count]['provinsi'] = $row->provinsi;
            $data_output['prop_baru'][$count]['is_premium'] = $row->isPremium;
            $data_output['prop_baru'][$count]['developer_id'] = $row->developer_id;
            $data_output['prop_baru'][$count]['tgl_upload']= $row->tgl_upload;

            //foto properti baru
            $sql = "select * from media where media.properti_baru_id = ?";
            $query = $this->db->query($sql, $row->id_properti_baru);
            $data_output['prop_baru'][$count]['picture'] = "";
            if ($query->num_rows() == 1) {
                $pic_prop_baru = $query->row();
                $data_output['prop_baru'][$count]['picture'] = $pic_prop_baru->url;
            }

            $sql = "select * from developer where developer.developer_id = ?";
            $query = $this->db->query($sql, $row->developer_id);
            $dev = $query->row();
            $data_output['prop_baru'][$count]['nama_developer'] = $dev->nama;

            $count++;
        }

        $data = array(
            'output' => $data_output,
            'page_title' => 'Properti Premium',
            'page_content' => 'properti_premium',
            'isLogin' => $islogin,
            'user' => $user,
            'provinsi' => $provinsi,
            'kota' => $kota);
        $this->load->view($this->_public_view, $data);
    }

    function detail() {
        $str_id = '';
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
        }

        if ($this->uri->segment(3) == FALSE) {
            redirect('properti_baru', 'location');
        } else {
            $str_id = $this->uri->segment(3);
        }

        $data = array('detail' => $this->mpropbaru->get_prop_detail($str_id),
            'page_content' => 'detail_properti_baru',
            'page_title' => 'Detail Properti Baru',
            'isLogin' => $islogin,
            'user' => $user);
        $this->load->view($this->_public_view, $data);
    }

}

?>
